"use strict";
var EleBasicSelect = require("../ele-basic-select");
function _interopDefaultLegacy(e) {
  return e && typeof e === "object" && "default" in e ? e : { "default": e };
}
var EleBasicSelect__default = /* @__PURE__ */ _interopDefaultLegacy(EleBasicSelect);
var render = function() {
  var _vm = this;
  var _h = _vm.$createElement;
  var _c = _vm._self._c || _h;
  return _c("EleBasicSelect", { ref: "select", attrs: { "value": _vm.value, "multiple": _vm.multiple, "disabled": _vm.disabled || _vm.loading, "size": _vm.size, "clearable": _vm.clearable, "collapse-tags": _vm.collapseTags, "placeholder": _vm.placeholder, "automatic-dropdown": _vm.automaticDropdown, "popper-class": _vm.selectPopperClass, "popper-options": _vm.popperOptions, "id": _vm.id, "name": _vm.name, "autocomplete": _vm.autocomplete, "selected-label": _vm.selectedLabel, "selected": _vm.selected, "visible": _vm.visible }, on: { "clear": _vm.onClear, "remove-tag": _vm.onRemoveTag, "focus": _vm.onFocus, "blur": _vm.onBlur, "update:visible": _vm.updateVisible, "after-enter": _vm.scrollToSelected } }, [_vm.filterable ? _c("div", { staticClass: "ele-tree-select-search" }, [_c("el-input", { attrs: { "clearable": "", "size": "mini", "placeholder": "\u641C\u7D22", "prefix-icon": "el-icon-search" }, on: { "input": _vm.filterNode }, model: { value: _vm.keywords, callback: function($$v) {
    _vm.keywords = $$v;
  }, expression: "keywords" } })], 1) : _vm._e(), _c("el-scrollbar", { ref: "scrollbar", attrs: { "wrap-class": "ele-tree-select-dropdown-wrap", "view-class": "ele-tree-select-dropdown-view" } }, [_c("el-tree", { ref: "tree", attrs: { "props": _vm.treeProps, "data": _vm.data, "node-key": _vm.valueKey, "show-checkbox": _vm.multiple, "highlight-current": !_vm.multiple, "expand-on-click-node": _vm.expandOnClickNode, "check-on-click-node": !_vm.expandOnClickNode, "filter-node-method": _vm.filterNodeMethod, "empty-text": _vm.noDataText, "render-after-expand": _vm.renderAfterExpand, "load": _vm.loadNode, "default-expand-all": _vm.defaultExpandAll, "auto-expand-parent": _vm.autoExpandParent, "default-expanded-keys": _vm.defaultExpandedKeys, "check-strictly": _vm.treeCheckStrictly, "accordion": _vm.accordion, "indent": _vm.indent, "icon-class": _vm.iconClass, "lazy": _vm.lazy }, on: { "node-click": _vm.onNodeClick, "check": _vm.onCheckedChange }, scopedSlots: _vm._u([{ key: "default", fn: function(ref) {
    var node = ref.node;
    var data = ref.data;
    return [_c("span", { staticClass: "el-tree-node__label ele-text-primary" }, [_vm._t("default", function() {
      return [_vm._v(_vm._s(data[_vm.labelKey]))];
    }, { "node": node, "data": data })], 2)];
  } }], null, true) })], 1)], 1);
};
var staticRenderFns = [];
function normalizeComponent(scriptExports, render2, staticRenderFns2, functionalTemplate, injectStyles, scopeId, moduleIdentifier, shadowMode) {
  var options = typeof scriptExports === "function" ? scriptExports.options : scriptExports;
  if (render2) {
    options.render = render2;
    options.staticRenderFns = staticRenderFns2;
    options._compiled = true;
  }
  if (functionalTemplate) {
    options.functional = true;
  }
  if (scopeId) {
    options._scopeId = "data-v-" + scopeId;
  }
  var hook;
  if (moduleIdentifier) {
    hook = function(context) {
      context = context || this.$vnode && this.$vnode.ssrContext || this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext;
      if (!context && typeof __VUE_SSR_CONTEXT__ !== "undefined") {
        context = __VUE_SSR_CONTEXT__;
      }
      if (injectStyles) {
        injectStyles.call(this, context);
      }
      if (context && context._registeredComponents) {
        context._registeredComponents.add(moduleIdentifier);
      }
    };
    options._ssrRegister = hook;
  } else if (injectStyles) {
    hook = shadowMode ? function() {
      injectStyles.call(this, (options.functional ? this.parent : this).$root.$options.shadowRoot);
    } : injectStyles;
  }
  if (hook) {
    if (options.functional) {
      options._injectStyles = hook;
      var originalRender = options.render;
      options.render = function renderWithStyleInjection(h, context) {
        hook.call(context);
        return originalRender(h, context);
      };
    } else {
      var existing = options.beforeCreate;
      options.beforeCreate = existing ? [].concat(existing, hook) : [hook];
    }
  }
  return {
    exports: scriptExports,
    options
  };
}
const __vue2_script = {
  name: "EleTreeSelect",
  components: { EleBasicSelect: EleBasicSelect__default["default"] },
  props: {
    value: [Number, String, Array],
    multiple: Boolean,
    disabled: Boolean,
    size: String,
    clearable: Boolean,
    collapseTags: Boolean,
    placeholder: String,
    filterable: Boolean,
    filterMethod: Function,
    remote: Boolean,
    noDataText: String,
    automaticDropdown: Boolean,
    popperClass: String,
    popperOptions: Object,
    id: String,
    name: String,
    autocomplete: {
      type: String,
      default: "off"
    },
    valueKey: {
      type: String,
      default: "value"
    },
    labelKey: {
      type: String,
      default: "label"
    },
    childrenKey: {
      type: String,
      default: "children"
    },
    disabledKey: {
      type: String,
      default: "disabled"
    },
    isLeafKey: {
      type: String,
      default: "isLeaf"
    },
    data: Array,
    renderAfterExpand: {
      type: Boolean,
      default: true
    },
    load: Function,
    defaultExpandAll: Boolean,
    expandOnClickNode: {
      type: Boolean,
      default: false
    },
    autoExpandParent: {
      type: Boolean,
      default: true
    },
    defaultExpandedKeys: Array,
    checkStrictly: Boolean,
    accordion: Boolean,
    indent: {
      type: Number,
      default: 16
    },
    iconClass: String,
    lazy: Boolean,
    initValue: [Object, Array]
  },
  emits: [
    "input",
    "change",
    "focus",
    "blur",
    "clear",
    "remove-tag",
    "visible-change",
    "search"
  ],
  data() {
    return {
      selectedLabel: "",
      selected: [],
      visible: false,
      keywords: "",
      treeData: this.data ? this.data : [],
      loading: false
    };
  },
  computed: {
    selectPopperClass() {
      const temp = ["ele-tree-select-dropdown"];
      if (this.popperClass) {
        temp.push(this.popperClass);
      }
      return temp.join(" ");
    },
    treeProps() {
      return {
        label: this.labelKey,
        children: this.childrenKey,
        disabled: this.disabledKey,
        isLeaf: this.isLeafKey
      };
    },
    treeCheckStrictly() {
      if (this.lazy) {
        return true;
      }
      return this.checkStrictly;
    }
  },
  methods: {
    updateVisible(visible) {
      this.visible = visible;
      this.$emit("visible-change", visible);
    },
    updateValue(value) {
      const changed = (() => {
        const isEmpty = this.isEmptyValue(this.value);
        if (!this.multiple) {
          if (this.isEmptyValue(value) && isEmpty) {
            return false;
          }
          return value !== this.value;
        }
        if (isEmpty || !this.value.length) {
          return !!value.length;
        } else if (value.length !== this.value.length) {
          return true;
        }
        for (let i = 0; i < value.length; i++) {
          if (!this.value.includes(value[i])) {
            return true;
          }
        }
        return false;
      })();
      if (changed) {
        this.$emit("input", value);
        this.$emit("change", value);
      }
    },
    onFocus() {
      this.$emit("focus");
    },
    onBlur() {
      this.$emit("blur");
    },
    onClear() {
      const value = this.multiple ? [] : null;
      this.updateValue(value);
      this.$emit("clear");
      this.updateVisible(false);
    },
    onRemoveTag(node) {
      var _a;
      const treeRef = (_a = this.$refs) == null ? void 0 : _a.tree;
      const value = node.value;
      treeRef == null ? void 0 : treeRef.setChecked(value, false, !this.treeCheckStrictly);
      const values = (() => {
        var _a2;
        if (this.treeCheckStrictly) {
          return this.value.filter((v) => v !== value);
        }
        return (_a2 = treeRef == null ? void 0 : treeRef.getCheckedKeys()) != null ? _a2 : [];
      })();
      this.updateValue(values);
      this.$emit("remove-tag", value);
    },
    onNodeClick(node) {
      var _a, _b;
      const treeRef = (_a = this.$refs) == null ? void 0 : _a.tree;
      const value = node[this.valueKey];
      const disabled = node[this.disabledKey];
      const isChild = this.lazy ? !!node[this.isLeafKey] : !((_b = node[this.childrenKey]) == null ? void 0 : _b.length);
      if (!this.multiple) {
        if (!disabled && (!this.expandOnClickNode || isChild)) {
          this.updateValue(value);
          this.updateVisible(false);
        } else if (this.isEmptyValue(this.value)) {
          treeRef == null ? void 0 : treeRef.setCurrentKey(null);
        } else {
          treeRef == null ? void 0 : treeRef.setCurrentKey(this.value);
        }
        return;
      }
      if (!disabled && this.expandOnClickNode && isChild) {
        const checked = !this.value.includes(value);
        treeRef == null ? void 0 : treeRef.setChecked(value, checked, !this.treeCheckStrictly);
        const values = (() => {
          var _a2;
          if (this.treeCheckStrictly) {
            if (checked) {
              return this.value.concat([value]);
            }
            return this.value.filter((v) => v !== value);
          }
          return (_a2 = treeRef == null ? void 0 : treeRef.getCheckedKeys()) != null ? _a2 : [];
        })();
        this.updateValue(values);
      }
    },
    onCheckedChange(node, { checkedKeys }) {
      if (this.multiple) {
        const values = this.value.filter((v) => {
          const temp = this.getNodeByValue(v);
          return temp ? checkedKeys.includes(v) : true;
        }).concat(checkedKeys.filter((v) => !this.value.includes(v)));
        this.updateValue(values);
      }
    },
    filterNodeMethod(value, data) {
      if (this.filterMethod) {
        return this.filterMethod(value, data);
      }
      if (!value) {
        return true;
      }
      return data[this.labelKey].includes(value);
    },
    filterNode() {
      var _a, _b;
      if (this.remote) {
        this.$emit("search", this.keywords);
      } else {
        (_b = (_a = this.$refs) == null ? void 0 : _a.tree) == null ? void 0 : _b.filter(this.keywords);
        this.$nextTick(() => {
          var _a2, _b2;
          (_b2 = (_a2 = this.$refs) == null ? void 0 : _a2.select) == null ? void 0 : _b2.updatePopper();
        });
      }
    },
    onValueChange(value) {
      var _a, _b, _c;
      const treeRef = (_a = this.$refs) == null ? void 0 : _a.tree;
      const noInit = this.isEmptyValue(this.initValue);
      if (!this.multiple) {
        if (this.isEmptyValue(value)) {
          this.selectedLabel = "";
          treeRef == null ? void 0 : treeRef.setCurrentKey(null);
          return;
        }
        const node = this.getNodeByValue(value);
        if (node) {
          this.selectedLabel = node[this.labelKey];
          treeRef == null ? void 0 : treeRef.setCurrentKey(value);
          return;
        }
        this.selectedLabel = noInit ? value : this.initValue[this.labelKey];
        treeRef == null ? void 0 : treeRef.setCurrentKey(null);
        return;
      }
      if (this.treeCheckStrictly) {
        this.selected = value.map((v) => {
          const label = (() => {
            const node = this.getNodeByValue(v);
            if (node) {
              return node[this.labelKey];
            }
            if (noInit) {
              return v;
            }
            const temp = this.initValue.find((t) => t[this.valueKey] === v);
            return temp ? temp[this.labelKey] : v;
          })();
          return {
            label,
            value: v
          };
        });
      } else {
        const checked = [];
        const eachData = (data) => {
          data == null ? void 0 : data.forEach((d) => {
            if (value.includes(d[this.valueKey])) {
              checked.push({
                label: d[this.labelKey],
                value: d[this.valueKey]
              });
            } else {
              eachData(d[this.childrenKey]);
            }
          });
        };
        eachData(this.treeData);
        this.selected = checked;
      }
      treeRef == null ? void 0 : treeRef.setCheckedKeys(value.filter((v) => {
        return !!this.getNodeByValue(v);
      }));
      (_c = (_b = this.$refs) == null ? void 0 : _b.select) == null ? void 0 : _c.resetInputHeight();
    },
    isEmptyValue(value) {
      return typeof value === "undefined" || value === null || value === "";
    },
    getNodeByValue(value, data) {
      if (!data && this.treeData) {
        return this.getNodeByValue(value, this.treeData);
      }
      let node;
      if (data == null ? void 0 : data.length) {
        for (let i = 0; i < data.length; i++) {
          if (value === data[i][this.valueKey]) {
            node = data[i];
            break;
          } else {
            const children = data[i][this.childrenKey];
            if (children) {
              node = this.getNodeByValue(value, children);
              if (typeof node !== "undefined") {
                break;
              }
            }
          }
        }
      }
      return node;
    },
    loadNode(node, resolve) {
      if (!node.data) {
        this.loading = true;
      }
      if (this.load) {
        this.load(node, (data) => {
          if (!node.data) {
            this.treeData = [].concat(data);
            if (this.multiple) {
              if (Array.isArray(this.value) && this.value.length) {
                this.onValueChange(this.value);
              }
            } else if (this.isEmptyValue(this.value)) {
              this.onValueChange(this.value);
            }
          } else {
            const item = this.getNodeByValue(node.data[this.valueKey]);
            item[this.childrenKey] = [].concat(data);
            this.$nextTick(() => {
              this.onValueChange(this.value);
            });
          }
          resolve(data);
          this.loading = false;
        });
      }
    },
    scrollToSelected() {
      var _a, _b, _c, _d, _e;
      const treeEl = (_b = (_a = this.$refs) == null ? void 0 : _a.tree) == null ? void 0 : _b.$el;
      if (!treeEl) {
        return;
      }
      const selector = this.multiple ? ".is-checked" : ".is-current";
      const el = treeEl.querySelector(".el-tree-node" + selector);
      if (el) {
        const wrapEl = (_c = treeEl.parentNode) == null ? void 0 : _c.parentNode;
        wrapEl.scrollTop = el.offsetTop - 40;
      }
      (_e = (_d = this.$refs) == null ? void 0 : _d.scrollbar) == null ? void 0 : _e.handleScroll();
    },
    onInitValueChange() {
      const hasInit = !this.isEmptyValue(this.initValue);
      if (hasInit) {
        if (this.multiple) {
          this.updateValue(this.initValue.map((d) => d[this.valueKey]));
        } else {
          this.updateValue(this.initValue[this.valueKey]);
        }
      }
    },
    getHalfCheckedNodes() {
      var _a, _b;
      return (_b = (_a = this.$refs) == null ? void 0 : _a.tree) == null ? void 0 : _b.getHalfCheckedNodes();
    },
    getHalfCheckedKeys() {
      var _a, _b;
      return (_b = (_a = this.$refs) == null ? void 0 : _a.tree) == null ? void 0 : _b.getHalfCheckedKeys();
    },
    getData() {
      return this.treeData;
    }
  },
  mounted() {
    var _a;
    const hasInit = !this.isEmptyValue(this.initValue);
    if (this.multiple) {
      if (hasInit) {
        this.onInitValueChange();
      } else if ((_a = this.value) == null ? void 0 : _a.length) {
        this.onValueChange(this.value);
      }
    } else if (hasInit) {
      this.onInitValueChange();
    } else if (!this.isEmptyValue(this.value)) {
      this.onValueChange(this.value);
    }
  },
  watch: {
    visible(visible) {
      if (visible) {
        this.$nextTick(() => {
          this.scrollToSelected();
        });
      }
    },
    value(value) {
      this.onValueChange(value);
    },
    initValue() {
      this.onInitValueChange();
    },
    checkStrictly() {
      this.onValueChange(this.value);
    },
    data(data) {
      this.treeData = data;
    }
  }
};
const __cssModules = {};
var __component__ = /* @__PURE__ */ normalizeComponent(__vue2_script, render, staticRenderFns, false, __vue2_injectStyles, null, null, null);
function __vue2_injectStyles(context) {
  for (let o in __cssModules) {
    this[o] = __cssModules[o];
  }
}
var index = /* @__PURE__ */ function() {
  return __component__.exports;
}();
module.exports = index;
